home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / opengl / utilities / isfast / libtk / shapes.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  12.2 KB  |  460 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include "tk.h"
  6. #include "private.h"
  7.  
  8. #if defined(__cplusplus) || defined(c_plusplus)
  9. #define class c_class
  10. #endif
  11.  
  12. /******************************************************************************/
  13.  
  14. #define PI 3.14159265358979323846
  15.  
  16. /******************************************************************************/
  17.  
  18. void tkWireSphere(GLuint base, float radius)
  19. {
  20.     GLUquadricObj *quadObj;
  21.  
  22.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  23.     quadObj = gluNewQuadric();
  24.     gluQuadricDrawStyle(quadObj, GLU_LINE);
  25.     gluSphere(quadObj, radius, 16, 16);
  26.     glEndList();
  27. }
  28.  
  29. /******************************************************************************/
  30.  
  31. void tkSolidSphere(GLuint base, float radius)
  32. {
  33.     GLUquadricObj *quadObj;
  34.  
  35.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  36.     quadObj = gluNewQuadric();
  37.     gluQuadricDrawStyle(quadObj, GLU_FILL);
  38.     gluQuadricNormals(quadObj, GLU_SMOOTH);
  39.     gluSphere(quadObj, radius, 16, 16);
  40.     glEndList();
  41. }
  42.  
  43. /******************************************************************************/
  44.  
  45. void tkWireCube(GLuint base, float size)
  46. {
  47.     static float n[6][3] = {
  48.     {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
  49.     {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0}
  50.     };
  51.     static GLint faces[6][4] = {
  52.     {0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
  53.     {4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3}
  54.     };
  55.     float x0, x1, y0, y1, z0, z1, tmp;
  56.     float v[8][3];
  57.     int i;
  58.  
  59.     x0 = -size / 2.0;
  60.     x1 = size / 2.0;
  61.     y0 = -size / 2.0;
  62.     y1 = size / 2.0;
  63.     z0 = -size / 2.0;
  64.     z1 = size / 2.0;
  65.  
  66.     if (x0 > x1) {
  67.     tmp = x0; x0 = x1; x1 = tmp;
  68.     }
  69.     if (y0 > y1) {
  70.     tmp = y0; y0 = y1; y1 = tmp; 
  71.     }
  72.     if (z0 > z1) {
  73.     tmp = z0; z0 = z1; z1 = tmp; 
  74.     }
  75.     v[0][0] = v[1][0] = v[2][0] = v[3][0] = x0;
  76.     v[4][0] = v[5][0] = v[6][0] = v[7][0] = x1;
  77.     v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
  78.     v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
  79.     v[0][2] = v[3][2] = v[4][2] = v[7][2] = z0;
  80.     v[1][2] = v[2][2] = v[5][2] = v[6][2] = z1;
  81.  
  82.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  83.     for (i = 0; i < 6; i++) {
  84.         glBegin(GL_LINE_LOOP);
  85.         glNormal3fv(&n[i][0]);
  86.         glVertex3fv(&v[faces[i][0]][0]);
  87.         glNormal3fv(&n[i][0]);
  88.         glVertex3fv(&v[faces[i][1]][0]);
  89.         glNormal3fv(&n[i][0]);
  90.         glVertex3fv(&v[faces[i][2]][0]);
  91.         glNormal3fv(&n[i][0]);
  92.         glVertex3fv(&v[faces[i][3]][0]);
  93.         glEnd();
  94.     }
  95.     glEndList();
  96. }
  97.  
  98. /******************************************************************************/
  99.  
  100. void tkSolidCube(GLuint base, float size)
  101. {
  102.     static float n[6][3] = {
  103.     {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
  104.     {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0}
  105.     };
  106.     static GLint faces[6][4] = {
  107.     {0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
  108.     {4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3}
  109.     };
  110.     float x0, x1, y0, y1, z0, z1, tmp;
  111.     float v[8][3];
  112.     int i;
  113.  
  114.     x0 = -size / 2.0;
  115.     x1 = size / 2.0;
  116.     y0 = -size / 2.0;
  117.     y1 = size / 2.0;
  118.     z0 = -size / 2.0;
  119.     z1 = size / 2.0;
  120.  
  121.     if (x0 > x1) {
  122.     tmp = x0; x0 = x1; x1 = tmp;
  123.     }
  124.     if (y0 > y1) {
  125.     tmp = y0; y0 = y1; y1 = tmp; 
  126.     }
  127.     if (z0 > z1) {
  128.     tmp = z0; z0 = z1; z1 = tmp; 
  129.     }
  130.     v[0][0] = v[1][0] = v[2][0] = v[3][0] = x0;
  131.     v[4][0] = v[5][0] = v[6][0] = v[7][0] = x1;
  132.     v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
  133.     v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
  134.     v[0][2] = v[3][2] = v[4][2] = v[7][2] = z0;
  135.     v[1][2] = v[2][2] = v[5][2] = v[6][2] = z1;
  136.  
  137.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  138.     for (i = 0; i < 6; i++) {
  139.         glBegin(GL_POLYGON);
  140.         glNormal3fv(&n[i][0]);
  141.         glVertex3fv(&v[faces[i][0]][0]);
  142.         glNormal3fv(&n[i][0]);
  143.         glVertex3fv(&v[faces[i][1]][0]);
  144.         glNormal3fv(&n[i][0]);
  145.         glVertex3fv(&v[faces[i][2]][0]);
  146.         glNormal3fv(&n[i][0]);
  147.         glVertex3fv(&v[faces[i][3]][0]);
  148.         glEnd();
  149.     }
  150.     glEndList();
  151. }
  152.  
  153. /******************************************************************************/
  154.  
  155. void tkWireBox(GLuint base, float width, float height, float depth)
  156. {
  157.     static float n[6][3] = {
  158.     {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
  159.     {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0}
  160.     };
  161.     static GLint faces[6][4] = {
  162.     {0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
  163.     {4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3}
  164.     };
  165.     float x0, x1, y0, y1, z0, z1, tmp;
  166.     float v[8][3];
  167.     int i;
  168.  
  169.     x0 = -width / 2.0;
  170.     x1 = width / 2.0;
  171.     y0 = -height / 2.0;
  172.     y1 = height / 2.0;
  173.     z0 = -depth / 2.0;
  174.     z1 = depth / 2.0;
  175.  
  176.     if (x0 > x1) {
  177.     tmp = x0; x0 = x1; x1 = tmp;
  178.     }
  179.     if (y0 > y1) {
  180.     tmp = y0; y0 = y1; y1 = tmp; 
  181.     }
  182.     if (z0 > z1) {
  183.     tmp = z0; z0 = z1; z1 = tmp; 
  184.     }
  185.     v[0][0] = v[1][0] = v[2][0] = v[3][0] = x0;
  186.     v[4][0] = v[5][0] = v[6][0] = v[7][0] = x1;
  187.     v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
  188.     v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
  189.     v[0][2] = v[3][2] = v[4][2] = v[7][2] = z0;
  190.     v[1][2] = v[2][2] = v[5][2] = v[6][2] = z1;
  191.  
  192.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  193.     for (i = 0; i < 6; i++) {
  194.         glBegin(GL_LINE_LOOP);
  195.         glNormal3fv(&n[i][0]);
  196.         glVertex3fv(&v[faces[i][0]][0]);
  197.         glNormal3fv(&n[i][0]);
  198.         glVertex3fv(&v[faces[i][1]][0]);
  199.         glNormal3fv(&n[i][0]);
  200.         glVertex3fv(&v[faces[i][2]][0]);
  201.         glNormal3fv(&n[i][0]);
  202.         glVertex3fv(&v[faces[i][3]][0]);
  203.         glEnd();
  204.     }
  205.     glEndList();
  206. }
  207.  
  208. /******************************************************************************/
  209.  
  210. void tkSolidBox(GLuint base, float width, float height, float depth)
  211. {
  212.     static float n[6][3] = {
  213.     {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
  214.     {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0}
  215.     };
  216.     static GLint faces[6][4] = {
  217.     {0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
  218.     {4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3}
  219.     };
  220.     float x0, x1, y0, y1, z0, z1, tmp;
  221.     float v[8][3];
  222.     int i;
  223.  
  224.     x0 = -width / 2.0;
  225.     x1 = width / 2.0;
  226.     y0 = -height / 2.0;
  227.     y1 = height / 2.0;
  228.     z0 = -depth / 2.0;
  229.     z1 = depth / 2.0;
  230.  
  231.     if (x0 > x1) {
  232.     tmp = x0; x0 = x1; x1 = tmp;
  233.     }
  234.     if (y0 > y1) {
  235.     tmp = y0; y0 = y1; y1 = tmp; 
  236.     }
  237.     if (z0 > z1) {
  238.     tmp = z0; z0 = z1; z1 = tmp; 
  239.     }
  240.     v[0][0] = v[1][0] = v[2][0] = v[3][0] = x0;
  241.     v[4][0] = v[5][0] = v[6][0] = v[7][0] = x1;
  242.     v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
  243.     v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
  244.     v[0][2] = v[3][2] = v[4][2] = v[7][2] = z0;
  245.     v[1][2] = v[2][2] = v[5][2] = v[6][2] = z1;
  246.  
  247.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  248.     for (i = 0; i < 6; i++) {
  249.         glBegin(GL_POLYGON);
  250.         glNormal3fv(&n[i][0]);
  251.         glVertex3fv(&v[faces[i][0]][0]);
  252.         glNormal3fv(&n[i][0]);
  253.         glVertex3fv(&v[faces[i][1]][0]);
  254.         glNormal3fv(&n[i][0]);
  255.         glVertex3fv(&v[faces[i][2]][0]);
  256.         glNormal3fv(&n[i][0]);
  257.         glVertex3fv(&v[faces[i][3]][0]);
  258.         glEnd();
  259.     }
  260.     glEndList();
  261. }
  262.  
  263. /******************************************************************************/
  264.  
  265. void tkWireTorus(GLuint base, float innerRadius, float outerRadius)
  266. {
  267.     GLint i, j;
  268.     float theta1, phi1, theta2, phi2, rings, sides;
  269.     float v0[03], v1[3], v2[3], v3[3];
  270.     float n0[3], n1[3], n2[3], n3[3];
  271.  
  272.     rings = 5;
  273.     sides = 10;
  274.  
  275.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  276.     for (i = 0; i < rings; i++) {
  277.     theta1 = (float)i * 2.0 * PI / rings;
  278.     theta2 = (float)(i + 1) * 2.0 * PI / rings;
  279.     for (j = 0; j < sides; j++) {
  280.         phi1 = (float)j * 2.0 * PI / sides;
  281.         phi2 = (float)(j + 1) * 2.0 * PI / sides;
  282.  
  283.         v0[0] = cos(theta1) * (outerRadius + innerRadius * cos(phi1));
  284.         v0[1] = -sin(theta1) * (outerRadius + innerRadius * cos(phi1));
  285.         v0[2] = innerRadius * sin(phi1);
  286.  
  287.         v1[0] = cos(theta2) * (outerRadius + innerRadius * cos(phi1));
  288.         v1[1] = -sin(theta2) * (outerRadius + innerRadius * cos(phi1));
  289.         v1[2] = innerRadius * sin(phi1);
  290.  
  291.         v2[0] = cos(theta2) * (outerRadius + innerRadius * cos(phi2));
  292.         v2[1] = -sin(theta2) * (outerRadius + innerRadius * cos(phi2));
  293.         v2[2] = innerRadius * sin(phi2);
  294.  
  295.         v3[0] = cos(theta1) * (outerRadius + innerRadius * cos(phi2));
  296.         v3[1] = -sin(theta1) * (outerRadius + innerRadius * cos(phi2));
  297.         v3[2] = innerRadius * sin(phi2);
  298.  
  299.         n0[0] = cos(theta1) * (cos(phi1));
  300.         n0[1] = -sin(theta1) * (cos(phi1));
  301.         n0[2] = sin(phi1);
  302.  
  303.         n1[0] = cos(theta2) * (cos(phi1));
  304.         n1[1] = -sin(theta2) * (cos(phi1));
  305.         n1[2] = sin(phi1);
  306.  
  307.         n2[0] = cos(theta2) * (cos(phi2));
  308.         n2[1] = -sin(theta2) * (cos(phi2));
  309.         n2[2] = sin(phi2);
  310.  
  311.         n3[0] = cos(theta1) * (cos(phi2));
  312.         n3[1] = -sin(theta1) * (cos(phi2));
  313.         n3[2] = sin(phi2);
  314.  
  315.         glBegin(GL_LINE_LOOP);
  316.         glNormal3fv(n3);
  317.         glVertex3fv(v3);
  318.         glNormal3fv(n2);
  319.         glVertex3fv(v2);
  320.         glNormal3fv(n1);
  321.         glVertex3fv(v1);
  322.         glNormal3fv(n0);
  323.         glVertex3fv(v0);
  324.         glEnd();
  325.     }
  326.     }
  327.     glEndList();
  328. }
  329.  
  330. /******************************************************************************/
  331.  
  332. void tkSolidTorus(GLuint base, float innerRadius, float outerRadius)
  333. {
  334.     GLint i, j;
  335.     float theta1, phi1, theta2, phi2, rings, sides;
  336.     float v0[03], v1[3], v2[3], v3[3];
  337.     float n0[3], n1[3], n2[3], n3[3];
  338.  
  339.     rings = 5;
  340.     sides = 10;
  341.  
  342.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  343.     for (i = 0; i < rings; i++) {
  344.     theta1 = (float)i * 2.0 * PI / rings;
  345.     theta2 = (float)(i + 1) * 2.0 * PI / rings;
  346.     for (j = 0; j < sides; j++) {
  347.         phi1 = (float)j * 2.0 * PI / sides;
  348.         phi2 = (float)(j + 1) * 2.0 * PI / sides;
  349.  
  350.         v0[0] = cos(theta1) * (outerRadius + innerRadius * cos(phi1));
  351.         v0[1] = -sin(theta1) * (outerRadius + innerRadius * cos(phi1));
  352.         v0[2] = innerRadius * sin(phi1);
  353.  
  354.         v1[0] = cos(theta2) * (outerRadius + innerRadius * cos(phi1));
  355.         v1[1] = -sin(theta2) * (outerRadius + innerRadius * cos(phi1));
  356.         v1[2] = innerRadius * sin(phi1);
  357.  
  358.         v2[0] = cos(theta2) * (outerRadius + innerRadius * cos(phi2));
  359.         v2[1] = -sin(theta2) * (outerRadius + innerRadius * cos(phi2));
  360.         v2[2] = innerRadius * sin(phi2);
  361.  
  362.         v3[0] = cos(theta1) * (outerRadius + innerRadius * cos(phi2));
  363.         v3[1] = -sin(theta1) * (outerRadius + innerRadius * cos(phi2));
  364.         v3[2] = innerRadius * sin(phi2);
  365.  
  366.         n0[0] = cos(theta1) * (cos(phi1));
  367.         n0[1] = -sin(theta1) * (cos(phi1));
  368.         n0[2] = sin(phi1);
  369.  
  370.         n1[0] = cos(theta2) * (cos(phi1));
  371.         n1[1] = -sin(theta2) * (cos(phi1));
  372.         n1[2] = sin(phi1);
  373.  
  374.         n2[0] = cos(theta2) * (cos(phi2));
  375.         n2[1] = -sin(theta2) * (cos(phi2));
  376.         n2[2] = sin(phi2);
  377.  
  378.         n3[0] = cos(theta1) * (cos(phi2));
  379.         n3[1] = -sin(theta1) * (cos(phi2));
  380.         n3[2] = sin(phi2);
  381.  
  382.         glBegin(GL_POLYGON);
  383.         glNormal3fv(n3);
  384.         glVertex3fv(v3);
  385.         glNormal3fv(n2);
  386.         glVertex3fv(v2);
  387.         glNormal3fv(n1);
  388.         glVertex3fv(v1);
  389.         glNormal3fv(n0);
  390.         glVertex3fv(v0);
  391.         glEnd();
  392.     }
  393.     }
  394.     glEndList();
  395. }
  396.  
  397. /******************************************************************************/
  398.  
  399. void tkWireCylinder(GLuint base, float radius, float height)
  400. {
  401.     GLUquadricObj *quadObj;
  402.  
  403.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  404.     glPushMatrix();
  405.     glRotatef(90.0, 1.0, 0.0, 0.0);
  406.     glTranslatef(0.0, 0.0, -1.0);
  407.     quadObj = gluNewQuadric();
  408.     gluQuadricDrawStyle(quadObj, GLU_LINE);
  409.     gluCylinder(quadObj, radius, radius, height, 12, 2);
  410.     glPopMatrix();
  411.     glEndList();
  412. }
  413.  
  414. /******************************************************************************/
  415.  
  416. void tkSolidCylinder(GLuint base, float radius, float height)
  417. {
  418.     GLUquadricObj *quadObj;
  419.  
  420.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  421.     glPushMatrix();
  422.     glRotatef(90.0, 1.0, 0.0, 0.0);
  423.     glTranslatef(0.0, 0.0, -1.0);
  424.     quadObj = gluNewQuadric();
  425.     gluQuadricDrawStyle(quadObj, GLU_FILL);
  426.     gluQuadricNormals(quadObj, GLU_SMOOTH);
  427.     gluCylinder(quadObj, radius, radius, height, 12, 2);
  428.     glPopMatrix();
  429.     glEndList();
  430. }
  431.  
  432. /******************************************************************************/
  433.  
  434. void tkWireCone(GLuint base, float b, float h)
  435. {
  436.     GLUquadricObj *quadObj;
  437.  
  438.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  439.     quadObj = gluNewQuadric();
  440.     gluQuadricDrawStyle(quadObj, GLU_LINE);
  441.     gluCylinder(quadObj, b, 0.0, h, 15, 10);
  442.     glEndList();
  443. }
  444.  
  445. /******************************************************************************/
  446.  
  447. void tkSolidCone(GLuint base, float b, float h)
  448. {
  449.     GLUquadricObj *quadObj;
  450.  
  451.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  452.     quadObj = gluNewQuadric();
  453.     gluQuadricDrawStyle(quadObj, GLU_FILL);
  454.     gluQuadricNormals(quadObj, GLU_SMOOTH);
  455.     gluCylinder(quadObj, b, 0.0, h, 15, 10);
  456.     glEndList();
  457. }
  458.  
  459. /******************************************************************************/
  460.